package com.cy.web;

import com.alibaba.fastjson2.JSONObject;
import com.cy.Service.CheckPointService;
import com.cy.Service.OrderrecordService;
import com.cy.bean.Fronthost;
import com.alibaba.fastjson2.JSON;
import com.cy.bean.Orderrecord;
import com.cy.util.Util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class OrderrecordController {

//    @Autowired
//   private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderrecordService orderrecordService;
    @Autowired
    private CheckPointService checkPointService;
    private int nownum;

    @RequestMapping("/addorder")
    public Orderrecord getGoods(@RequestBody Orderrecord orderrecord){
         nownum += 1;
//        Orderrecord orderrecord = new Orderrecord();
        System.out.println("获取用户openid：" + orderrecord.getOpenid()+"。将去请求业务服务器得到用户信息");
        String res = restTemplate.getForObject("http://service-pay/getfonthost/"+orderrecord.getOpenid(),String.class);
        System.out.println(res);
        Map  map= JSONObject.parseObject(res,Map.class);
       // ObjectMapper objectMapper = new ObjectMapper();
      //  Map<String,Fronthost> map = objectMapper.readValue(res,Map<String,Fronthost>.class);
        String port = (String) map.get("port");
        System.out.println("获取服务端口："+port);
//        System.out.println(map.get("user")+":"+map.get("user").getClass());
        //System.out.println(map.get("user").toString());
        Object userobj = map.get("user");
        System.out.println(userobj);
//        System.out.println(so);
//        String s = JSONObject.toJSONString(so);
//        System.out.println(s);
//JSONObject jsonObject = JSONObject.parseObject(so);
        Fronthost user =JSONObject.parseObject(((JSONObject)userobj).toJSONString(),Fronthost.class);
//        JSONObject user = JSONObject.parseObject(map.get("user").toString());
//        orderrecord.setUid((Long) user.get("uid"));
//        Fronthost user = JSON.parseObject(map.get("user").toString(),Fronthost.class);
        orderrecord.setUid(user.getUid());
        orderrecord.setOstatus("0");
        orderrecord.setNowdate(Util.getData());
//        //将预约存redis
        Jedis jedis = new Jedis("43.143.104.112",6379);
        System.out.println("链接成功"+jedis.ping());
        jedis.lpush("orderrecords",JSON.toJSONString(orderrecord));
        //if判断redis存的数据是否达到50个，到了之后批量添加将数据库
        if(nownum>=10){
            List<String> list = jedis.lrange("orderrecords",0,-1);
            List<Orderrecord> orderList = JSON.parseArray(String.valueOf(list), Orderrecord.class);//对象列表
            //去数据库用sql获取符合条件的信息 ，返回list<OrderRecord>
            //Jedis jedis = new Jedis("43.143.104.112",6379);
            //List<String> list = jedis.lrange("orderrecords",0,-1);
            //List<Orderrecord> orderList = JSON.parseArray(String.valueOf(list), Orderrecord.class);
            //循环orderlist判断并且把符合条件的加入数据库获得的list里
            //返回给前端
            if(orderrecordService.addOrderrecords((ArrayList<Orderrecord>) orderList)>0){
                jedis.del("orderrecords");
            }
        }
        return orderrecord;
    }
    @RequestMapping("/getnownumber")
    public int getNownum(@RequestParam int markerId,@RequestParam String nowdate){
        int nowNum=0;
        nowNum+=checkPointService.getnownum(markerId, nowdate);

        Jedis jedis = new Jedis("43.143.104.112",6379);
        List<String> list = jedis.lrange("orderrecords",0,-1);
        List<Orderrecord> orderList = JSON.parseArray(String.valueOf(list), Orderrecord.class);

        System.out.println(orderList);
        nowNum+=orderList.size();
        return nowNum;
    }
}
